import {BaseEle, createUUID} from "./core.js";

let ZTextMap = {};

let hiddendiv = document.getElementById('ztextcon_fix')
if (!hiddendiv) {
    let cacheSet = new Set();
    hiddendiv = document.createElement("div");
    hiddendiv.setAttribute("id", "ztextcon_fix");
    hiddendiv.setAttribute("hidden", "true");

    hiddendiv.add =function (ele) {
        if (!cacheSet.has(ele)) {
            cacheSet.add(ele);
            hiddendiv.append(ele)
        }
    }
    document.body.append(hiddendiv);
}

export class ZText extends BaseEle {
    constructor() {
        super();
        let self = this;

        let id = createUUID();
        let textNode = document.createTextNode("text");


        textNode.id = id
        this.setAttribute('hidden', 'true');
        this.after(textNode);
        ZTextMap[id] = textNode;
        this.textNode = textNode;
        // this.remove()
        //
    }
    /**
     *
     * @param v {string}
     * @returns {string}
     */
    handleValue(v) {
        return v
    }
    mounted() {
        this._log('webcom Component text mounted',  this.getAttribute('value' ));
        this.textNode.textContent =  this.handleValue(this.getAttribute('value' ) ?? this.textContent)
    }
    attrChanged(name, oldValue, newValue) {
        this._log("attrChanged attribute", name, oldValue, newValue);
        if (name === 'value') {
            hiddendiv.add(this);
            this.textNode.textContent =  this.handleValue(newValue);
        }
    }
}

ZText.defsel('z-text',{
    attrs: {
        value: ''
    }
})